package 
{
	import org.flixel.FlxPoint;
	import org.flixel.FlxSound;
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.plugin.photonstorm.FlxControl;
	import org.flixel.plugin.photonstorm.FlxControlHandler;
	
	public class Player extends FlxSprite
	{
		[Embed(source = '../assets/playerInicial.png')] private var playerIniciallPNG:Class;
		
		private var jumpFX:FlxSound;
		private var walkFX:FlxSound;
		private var start:FlxPoint;
		private var controle:int = 0;
		
		public function Player(x:Number, y:Number)
		{
			//	As this extends FlxSprite we need to call super() to ensure all of the parent variables we need are created
			super(x, y);
			
			start = new FlxPoint(x, y);
			
			jumpFX = new FlxSound();
			jumpFX.loadEmbedded(jumpSFX);
			
			walkFX = new FlxSound();
			walkFX.loadEmbedded(walkSFX, true);	
			
			//	Load the player.png into this sprite.
			//	The 2nd parameter tells Flixel it's a sprite sheet and it should chop it up into 16x18 sized frames.
			loadGraphic(playerIniciallPNG, true, true, 22, 38
			, true);
			
			//	Because we've shaved a few pixels off, we need to offset the sprite to compensate
			offset.y = 4;
			
			//	Enable the Controls plugin - you only need do this once (unless you destroy the plugin)
			if (FlxG.getPlugin(FlxControl) == null)
			{
				FlxG.addPlugin(new FlxControl);			
			}
			
			//	Add this sprite to the FlxControl plugin and tell it we want the sprite to accelerate and decelerate smoothly
			FlxControl.create(this, FlxControlHandler.MOVEMENT_ACCELERATES, FlxControlHandler.STOPPING_DECELERATES, 1, true, false);
			
			//	Sprite will be controlled with the left and right cursor keys
			FlxControl.player1.setCursorControl(false, false, true, true);
			
			//inializa a o player com o asset e speeds
			init();
			
			//	Set a downward gravity of 400px/sec
			FlxControl.player1.setGravity(0, 400);
			
			FlxControl.player1.setSounds(jumpFX, null, walkFX);
			
			
			//	By default the sprite is facing to the right.
			//	Changing this tells Flixel to flip the sprite frames to show the left-facing ones instead.
			facing = FlxObject.RIGHT;
		}
		
		//	We don't actually kill the player at all, we just reset them
		public function restart():void
		{
			reset(start.x, start.y);
		}
		
		public function change(controle:int):void
		{			
			if (controle <= 0) {
				init();		
				
			}else if (controle > 0 && controle <= 1) {
				initGelo();
				
			}else if (controle > 1 && controle <= 2) {
				initFire();
				
			}
		}
		
		
		
		private function init():void
		{
		
			this._animations = new Array();
			//	The Animation sequences we need
			addAnimation("idle", [0], 0, false);
			addAnimation("walk", [0, 1, 0, 2], 10, true);
			addAnimation("jump", [1], 0, false);
			addAnimation("hurt", [3], 0, false);
			
			//	The sprite is 32x36 in size, but that includes a little feather of hair on its head which we don't want to include in collision checks.
			//	We also shave 2 pixels off each side to make it slip through gaps easier. Changing the width/height does NOT change the visual sprite, just the bounding box used for physics.
			width = 22;
			height = 32;
			
			FlxControl.player1.setMovementSpeed(800, 0, 200, 300, 300, 0);
			
			//	And SPACE BAR will make them jump up to a maximum of 400 pixels (per second), only when touching the FLOOR
			FlxControl.player1.setJumpButton("SPACE", FlxControlHandler.KEYMODE_PRESSED, 300, FlxObject.FLOOR, 250, 0);
		}
		
		private function initGelo():void
		{
			this._animations = new Array();
			//	The Animation sequences we need
			addAnimation("idle", [4], 0, false);
			addAnimation("walk", [4, 5, 4, 6], 10, true);
			addAnimation("jump", [5], 0, false);
			addAnimation("hurt", [7], 0, false);
			
			width = 22;
			height = 32;
			FlxControl.player1.setMovementSpeed(600, 0, 200, 400, 200, 0);
			FlxControl.player1.setJumpButton("SPACE", FlxControlHandler.KEYMODE_PRESSED, 400, FlxObject.FLOOR, 250, 0);
		}
		
		public function jump():void
		{
			var p1:FlxControlHandler = FlxControl.player1;
			
			velocity.y *= -1;
		}
		
		private function initFire():void
		{
			
			this._animations = new Array();
			//	The Animation sequences we need
			addAnimation("idle", [8], 0, false);
			addAnimation("walk", [8, 9, 8, 10], 10, true);
			addAnimation("jump", [9], 0, false);
			addAnimation("hurt", [11], 0, false);
			
			width = 22;
			height = 32;
			FlxControl.player1.setMovementSpeed(1000, 0, 400, 300, 400, 0);
			FlxControl.player1.setJumpButton("SPACE", FlxControlHandler.KEYMODE_PRESSED, 200, FlxObject.FLOOR, 250, 0);
		}
		
		override public function update():void
		{
			super.update();
			
			if (x < 0)
			{
				x = 0;
			}
			
			//	Have they hit the water?
			if (y > 536)
			{
				restart();
			}
			
			if (touching == FlxObject.FLOOR)
			{
				if (velocity.x != 0)
				{
					play("walk");
				}
				else
				{
					play("idle");
				}
			}
			else if (velocity.y < 0)
			{
				play("jump");
			}
		}
		
	}
}